package com.ivan.stu.notetool.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by IVAN on 2019/2/26.
 */

public class HttpDownLoad {

    private int mTimeoutMillis = 10 * 1000;
    private OnDownloadListener mListener;

    public void setTimeoutMillis(int timeoutMillis) {
        mTimeoutMillis = timeoutMillis;
    }

    public void setOnDownloadListener(OnDownloadListener listener) {
        mListener = listener;
    }

    public void download(String remoteUrl, File localFile) {
        if (localFile == null) {
            if (mListener != null) {
                mListener.onFail("You local file object is null, please check it.");
            }
            return;
        }
        HttpURLConnection conn = null;
        InputStream is = null;
        OutputStream os = null;
        try {
            URL url = new URL(remoteUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(mTimeoutMillis);
            int code = conn.getResponseCode();
            if (code == HttpURLConnection.HTTP_OK) {
                is = conn.getInputStream();

                long total;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    total = conn.getContentLengthLong();
                } else {
                    total = conn.getContentLength();
                }

                long skipLen = localFile.length();
                if (total>0){

                    if (skipLen >= total) {
                        if (mListener != null) mListener.onCompleted(skipLen, total);
                        return;
                    }
                }

                long current = 0;

                if (skipLen > 0 && skipLen < total) {
                    current = is.skip(skipLen);
                }
                if (mListener != null) mListener.onStart(current, total);

                os = new FileOutputStream(localFile, true);

                byte[] buffer = new byte[8 * 1024];
                int length;
                while ((length = is.read(buffer)) != -1) {
                    os.write(buffer, 0, length);
                    current += length;
                    if (mListener != null) mListener.onDownloading(current, total);
                }
                os.flush();

                if (mListener != null) {

                    if (total>0){

                        mListener.onCompleted(current, total);
                    }


                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (mListener != null) mListener.onFail(e.getMessage());
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public interface OnDownloadListener {
        void onStart(long current, long total);

        void onDownloading(long current, long total);

        void onCompleted(long current, long total);

        void onFail(String msg);
    }
}
